//
//  MainViewController.m
//  weibo-OC
//
//  Created by diyuyma on 15/12/11.
//  Copyright © 2015年 zhangkaiyun. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewController.h"
#import "DisCoverTableViewController.h"
#import "MessageTableViewController.h"
#import "ProfileTableViewController.h"
#import "MainTabBar.h"
#import "ComposeViewController.h"
#import "BaseNavViewController.h"

@interface MainViewController ()

//@property (nonatomic, weak)MainTabBar *mainTabBar;
@property (nonatomic, strong)ComposeViewController *composeViewController;
@end

@implementation MainViewController
- (ComposeViewController *)composeViewController {
    if (_composeViewController == nil) {
        _composeViewController = [[ComposeViewController alloc] init];
    }
    return _composeViewController;
}

- (void)composeDidClick {
//    ComposeViewController *composeViewController = [[ComposeViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.composeViewController];
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 由于 tabbar 是只读的 不能偶直接操作 如果要修改 可以使用 KVC
    MainTabBar *mainTabBar = [[MainTabBar alloc] init];

    // KVC 赋值
    [self setValue:mainTabBar forKey:@"tabBar"];
    
    // 添加自动控制器到视图上
    [self addChildViewControllers];
    
    // classForCoder 打印函数的类名
//    NSLog(@"%@", self.tabBar.classForCoder);
    
    [mainTabBar.composeBtn addTarget:self action:@selector(composeDidClick) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - TabBarDelegate 代理方法
- (void)tabBarDidClickPluseButton:(MainTabBar *)tabBar {
    
    ComposeViewController *composeViewController = [[ComposeViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:composeViewController];
    [self presentViewController:navigationController animated:YES completion:nil];
}

- (void)addChildViewControllers {
    
    [self addChildViewController:[[HomeTableViewController alloc] init] addTitle:[NSString stringWithFormat:@"首页"] addImageName: [UIImage imageNamed:@"tabbar_home"]];
    [self addChildViewController:[[MessageTableViewController alloc] init] addTitle:[NSString stringWithFormat:@"消息"] addImageName: [UIImage imageNamed:@"tabbar_message_center"]];
    [self addChildViewController:[[DisCoverTableViewController alloc] init] addTitle:[NSString stringWithFormat:@"发现"] addImageName: [UIImage imageNamed:@"tabbar_discover"]];
    [self addChildViewController:[[ProfileTableViewController alloc] init] addTitle:[NSString stringWithFormat:@"我"] addImageName: [UIImage imageNamed:@"tabbar_profile"]];

}

- (void)addChildViewController: (UITableViewController *)vc addTitle: (NSString *)title addImageName: (UIImage *)imageName {
    
//    self.tabBar.tintColor = [UIColor orangeColor];
    // 实例化导航视图控制器
    BaseNavViewController *nav = [[BaseNavViewController alloc] initWithRootViewController:vc];
    
    //    nav.title = [NSString stringWithFormat:@"首页"];
    
    vc.title = title;
    
    vc.tabBarItem.image = imageName;
    
//    NSString *ab = [NSString stringWithFormat:@"%@_highlighted", title];
//    
//    NSLog(@"%@", ab);
//    
//    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted", title]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    
//    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]} forState: UIControlStateSelected];
    
    [self addChildViewController:nav];
    
}
@end
